package com.wink_172.library.view

import android.content.Context
import android.graphics.Typeface
import android.text.*
import android.util.AttributeSet
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import com.wink_172.library.callback.ICallback
import java.util.regex.Pattern
import java.util.regex.PatternSyntaxException

open class CustomEditText : EditText {
    var  mInputMethodManager: InputMethodManager? = null
    fun setCallback(callback: ICallback?) {
        mCallback = callback
    }

    protected var mCallback: ICallback? = null



    constructor(context: Context?) : super(context)

    override fun onFinishInflate() {
        super.onFinishInflate()
        initView()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    private fun initView() {
        mInputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager //befor

//        动态设置文字颜色
//        setTextColor(getResources().getColorStateList(R.drawable.selector_text_color));

//        //清除焦点 默认不被选中
//        clearFocus();
//        setSelected(false);

//        addTextChangedListener(object : TextWatcher {
//            override fun beforeTextChanged(var1: CharSequence?, var2: Int, var3: Int, var4: Int) {
//
//            }
//
//            override fun onTextChanged(var1: CharSequence?, var2: Int, var3: Int, var4: Int) {
//
//            }
//
//            override fun afterTextChanged(var1: Editable?) {
//
//            }
//        })
    }

    fun hideSoftInput() {
        if (mInputMethodManager != null) {
            mInputMethodManager!!.hideSoftInputFromWindow(applicationWindowToken, 0)
        }
    }

    fun showSoftInput() {
        if (mInputMethodManager != null) {
            requestFocus()
            mInputMethodManager!!.showSoftInput(this, InputMethodManager.SHOW_FORCED)
        }
    }

    fun inputNum() {
        inputType = InputType.TYPE_CLASS_NUMBER
    }

    fun inputDecimalNum() {
        inputType = 8194
        //        8194就是android:inputType="numberDecimal"，
    }

    @Throws(PatternSyntaxException::class)
    fun stringFilter(str: String?): String {
        // 只允许字母、数字和汉字      
        val regEx = "[^a-zA-Z0-9\u4E00-\u9FA5]"
        val p = Pattern.compile(regEx)
        val m = p.matcher(str)
        return m.replaceAll("").trim { it <= ' ' }
    }

    /**
     * 设置禁止输入空格和换行符
     */
    fun setCustomerFilters() {
        val filter = InputFilter { source, start, end, dest, dstart, dend ->
            if (source == " " || source.toString().contentEquals("\n")) "" else null
        }
        filters = arrayOf(filter)
    }

    /**
     * 将输入框光标移动到末尾
     */
    fun setSelectionEnd() {
        val text: CharSequence = text
        if (text is Spannable) {
            Selection.setSelection(text, text.length)
        }
    }

    /**
     * 输入框是否明文可见
     *
     */
    private fun passInputVisiable(show: Boolean) {
        inputType = if (show) {
            //选择状态 显示明文--设置为可见的密码
            InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
        } else {
            InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
        }
        typeface = Typeface.DEFAULT
    }

    companion object {
        const val EVENT_KEYBOARD_CHANGE = 300
        const val EVENT_CHECK_VALUE = 301
        private const val TAG = "TAG"
    }
}